<?php
/**
 * 管理画面コントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Manage_Audiostory extends  Controller {
	
        /*
	 * index
	 * @access  public
	 * @return  Response
	 */
	public function action_index() {
                
                $view = array();
                $query = DB::select('*');
                $query->from('bk_audio_story');
                $result = $query->execute()->as_array();
                $view['col'] = $result; 
                return Response::forge(View::forge('manage/audiostory/index',$view), D_HTTP_UPDATE);
	}

	/*
	 * left
         * @access  public
	 * @return  Response
	 */
	public function action_add() {
                
                $post = Input::post();
                $view = array();
                $view['message'] ='';
                
                $query = DB::select('*');
                $query->from('bk_catagories');
                $rs = $query->execute()->as_array();
                $view['category'] =$rs;
                
                $query = DB::select('*');
                $query->from('bk_story');
                $rs = $query->execute()->as_array();
                $view['story'] =$rs;
                
                if(isset($post['action']))
                {
                    $param = array();
                    $param['title'] = $post['title'];
                    $param['category_id'] = $post['catagory'];
                    $param['story_id']=isset($post['story'])?$post['story']:'';
                    $param['full_link'] =  isset($post['link1'])?$post['link1']:'';
                    $param['demo_link'] =isset($post['link2'])?$post['link2']:'';
                    $param['avatar_link'] =isset($post['avatar'])?$post['avatar']:'';       
                    $param['order'] =isset($post['order'])?$post['order']:'';
                    $param['duration'] =isset($post['duration'])?$post['duration']:'';
                    $param['type_status'] =isset($post['status'])?$post['status']:'';
                    $param['member_id'] ='admin';
                    $param['access_type'] =isset($post['type_access'])?$post['type_access']:'';
                    $param['active'] =isset($post['active'])?$post['active']:'0';
                    $param['description'] =isset($post['desc'])?$post['desc']:'';
                    
                    $query = DB::insert('bk_audio_story');
                    
                    $query -> set($param);
                    $query -> execute();
                    
                   // var_dump($post);
                    $view['message']='successfully';
                 //   return;
                }
                
		return Response::forge(View::forge('manage/audiostory/addnew',$view), D_HTTP_UPDATE);
	}
        public function action_submit()
        {
            
            return Response::redirect('/manage/admin/index');
            
        } 
        /*
         * catagory 
         */
        public function action_search()
        {
            $post=  Input::post();
            $view = array();
            
            if(isset($post['search']))
            {   
                $q = isset($post['q'])?$post['q']:'';
                
                $query = DB::select('*');
		$query -> from('bk_audio_story');
		$query -> where(DB::expr('MATCH(title)'), '', DB::expr("AGAINST( " . DB::escape($q) . " IN BOOLEAN MODE)"));
		$result = $query -> execute() -> as_array();
                $view['data'] = $result;
              
              
                return Response::forge(View::forge('manage/audiostory/search', $view), D_HTTP_UPDATE);
                
            }  else {
                return Response::forge(View::forge('manage/audiostory/search'), D_HTTP_UPDATE);
            }
            
            
        }
        public function action_todo()
        {
            echo 'Đang phát triển xin vui lòng đợi thêm thời gian!!!';
            
        }
        public function action_del()
        {
                //$view = array();
                $get = Input::get();
                $query = DB::delete('bk_audio_story');
                $query ->where('id',$get['id']);
                $query->execute();
               
                return Response::redirect('manage/audiostory/');
        }
	public function action_modify()
        {
                $post = Input::post();
                $view = array();
                $view['message'] ='';
                if(isset($post['action']))
                {
                    $param = array();
                    $param['title'] = $post['title'];
                    $param['category_id'] = $post['catagory'];
                    $param['story_id']=isset($post['story'])?$post['story']:'';
                    $param['full_link'] =  isset($post['link1'])?$post['link1']:'';
                    $param['demo_link'] =isset($post['link2'])?$post['link2']:'';
                    $param['avatar_link'] =isset($post['avatar'])?$post['avatar']:'';       
                    $param['order'] =isset($post['order'])?$post['order']:'';
                    $param['duration'] =isset($post['duration'])?$post['duration']:'';
                    $param['type_status'] =isset($post['status'])?$post['status']:'';
                    $param['member_id'] ='admin';
                    $param['access_type'] =isset($post['type_access'])?$post['type_access']:'';
                    $param['active'] =isset($post['active'])?$post['active']:'0';
                    $param['description'] =isset($post['desc'])?$post['desc']:'';
                    $query = DB::update('bk_audio_story');
                    $query->set($param);
                    $query->where('id',$post['id']);
                    $query->execute();
                    
                  
                }
                else
                {
                    $get = Input::get();
                    $id = $get['id'];
                    $query = DB::select('*');
                    $query->from('bk_audio_story');
                    $query->where('id',$id);
                    $data=$query->execute()->as_array();
                    $view['data']=$data;
                    
                    $query = DB::select('*');
                    $query->from('bk_catagories');
                    $rs = $query->execute()->as_array();
                    $view['category'] =$rs;
                    
                    return Response::forge(View::forge('manage/audiostory/edit',$view), D_HTTP_UPDATE);
                }
             return Response::redirect('manage/audiostory/');
        }

        
	

}
